package de.elmar_baumann.jbirthdays.ui;

import de.elmar_baumann.jbirthdays.api.BirthdaysUtil;
import de.elmar_baumann.jbirthdays.api.PersonRepository;
import java.awt.Component;
import java.awt.GridBagConstraints;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import org.openide.util.Lookup;

/**
 * @author Elmar Baumann
 */
public class RepositoriesPanel extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;
    private final RepoModel repoModel = new RepoModel();

    public RepositoriesPanel() {
        initComponents();
        postInitComponents();
    }

    private void postInitComponents() {
        setSelectedRepo();
        comboBoxRepositories.setEnabled(repoModel.getSize() > 1);
    }

    private void setSelectedRepo() {
        panelRepoComponent.removeAll();
        PersonRepository selectedRepo = (PersonRepository) comboBoxRepositories.getSelectedItem();
        if (selectedRepo != null) {
            addToRepoComponent(selectedRepo.getSettingsComponent());
            boolean isUniqueRepo = repoModel.getSize() < 2;
            checkBoxPreferred.setSelected(isUniqueRepo || BirthdaysUtil.isPreferredRepository(selectedRepo));
            checkBoxPreferred.setEnabled(!isUniqueRepo);
        }
    }

    private void addToRepoComponent(Component c) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        panelRepoComponent.add(c, gbc);
    }

    private static final class RepoModel extends DefaultComboBoxModel<PersonRepository> {

        private static final long serialVersionUID = 1L;

        private RepoModel() {
            addRepositories();
        }

        private void addRepositories() {
            for (PersonRepository repo : Lookup.getDefault().lookupAll(PersonRepository.class)) {
                addElement(repo);
            }
        }
    }

    private final ListCellRenderer<PersonRepository> repoCellRenderer = new ListCellRenderer<PersonRepository>() {

        private final DefaultListCellRenderer delegate = new DefaultListCellRenderer();

        @Override
        public Component getListCellRendererComponent(JList<? extends PersonRepository> list, PersonRepository value, int index, boolean isSelected, boolean cellHasFocus) {
            JLabel label = (JLabel) delegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value != null) {
                label.setText(value.getDisplayName());
            }
            return label;
        }
    };

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        labelRepositories = new javax.swing.JLabel();
        comboBoxRepositories = new javax.swing.JComboBox<>();
        checkBoxPreferred = new javax.swing.JCheckBox();
        panelRepoComponent = new javax.swing.JPanel();
        panelFill = new javax.swing.JPanel();

        setLayout(new java.awt.GridBagLayout());

        labelRepositories.setLabelFor(comboBoxRepositories);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("de/elmar_baumann/jbirthdays/ui/Bundle"); // NOI18N
        labelRepositories.setText(bundle.getString("RepositoriesPanel.labelRepositories.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 0);
        add(labelRepositories, gridBagConstraints);

        comboBoxRepositories.setModel(repoModel);
        comboBoxRepositories.setRenderer(repoCellRenderer);
        comboBoxRepositories.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBoxRepositoriesActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 0, 0);
        add(comboBoxRepositories, gridBagConstraints);

        checkBoxPreferred.setText(bundle.getString("RepositoriesPanel.checkBoxPreferred.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 0, 10);
        add(checkBoxPreferred, gridBagConstraints);

        panelRepoComponent.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        add(panelRepoComponent, gridBagConstraints);

        javax.swing.GroupLayout panelFillLayout = new javax.swing.GroupLayout(panelFill);
        panelFill.setLayout(panelFillLayout);
        panelFillLayout.setHorizontalGroup(
            panelFillLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panelFillLayout.setVerticalGroup(
            panelFillLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.weighty = 1.0;
        add(panelFill, gridBagConstraints);
    }//GEN-END:initComponents

    private void comboBoxRepositoriesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboBoxRepositoriesActionPerformed
        setSelectedRepo();
    }//GEN-LAST:event_comboBoxRepositoriesActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox checkBoxPreferred;
    private javax.swing.JComboBox<PersonRepository> comboBoxRepositories;
    private javax.swing.JLabel labelRepositories;
    private javax.swing.JPanel panelFill;
    private javax.swing.JPanel panelRepoComponent;
    // End of variables declaration//GEN-END:variables

}
